﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;

namespace DuJianliang.ApplicationPlatform.Reimb
{
    public partial class ReimbList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }

        private void BindGridView()
        {
            string sql = @"SELECT A.*, C.FlowID, B.StepID, B.StepName, B.OperationName, B.OperationDate, B.OperatorInfo, B.Completed
                            FROM dbo.Reimb A 
                            LEFT JOIN dbo.FlowInstance B ON A.FlowInstanceID = B.FlowInstanceID
                            LEFT JOIN dbo.FlowMain C ON C.TableName = 'Reimb'
                            ORDER BY CreateDate DESC";

            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

            gvReimb.DataSource = dataTable;
            gvReimb.DataBind();
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("ReimbEdit.aspx?Source=list");
        }

        protected void gvReimb_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(e.CommandName) || e.CommandName.ToLower().Equals("page"))
            {
                return;
            }

            GridViewRow gridViewRow = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));
            string flowInstanceID = gvReimb.DataKeys[gridViewRow.RowIndex]["FlowInstanceID"].ToString();           

            switch (e.CommandName)
            { 
                case "CmdEdit" :
                    Response.Redirect(string.Format("ReimbEdit.aspx?flowInstanceID={0}&Source=list", flowInstanceID));
                    break;
                case "CmdShowFlow" :
                    BindGridView();
                    break;
                case "CmdDelete" :
                    this.Delete(flowInstanceID);
                    BindGridView();
                    break;
                default:
                    break;
            }
        }

        protected void gvReimb_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string flowID = gvReimb.DataKeys[e.Row.RowIndex]["FlowID"].ToString();
                string flowInstanceID = gvReimb.DataKeys[e.Row.RowIndex]["FlowInstanceID"].ToString();

                TableCell tableCell_FlowCompleted = e.Row.Cells[9];
                string flowCompleted = tableCell_FlowCompleted.Text.ToLower();

                switch (flowCompleted)
                { 
                    case "true" :
                        tableCell_FlowCompleted.Text = "已完成";
                        e.Row.BackColor = System.Drawing.Color.LightGreen;
                        break;
                    case "false" :
                        tableCell_FlowCompleted.Text = "处理中";
                        e.Row.BackColor = System.Drawing.Color.Pink;
                        break;
                    default:
                        break;
                }

                Button btnFlow = e.Row.Cells[10].FindControl("btnFlow") as Button;
                if (btnFlow != null)
                {                   
                    string url = string.Format("/Flow/FlowManager.aspx?FlowID={0}&FlowInstanceID={1}", flowID, flowInstanceID);
                    btnFlow.Attributes.Add("onclick", string.Format("showPage('{0}');", url));
                }

                Button btnDelete = e.Row.Cells[10].FindControl("btnDelete") as Button;
                if (btnDelete != null)
                {

                    if (!FlowHelper.HasFlowData(new Guid(flowInstanceID)))
                    {
                        btnDelete.Attributes.Add("onclick", "javascript:return confirm('确定要删除吗?')");
                    }
                    else
                    {
                        btnDelete.Visible = false;
                    }    
                }                            
            }
        }

        private void Delete(string flowInstanceID)
        {
            if (FlowHelper.HasFlowData(new Guid(flowInstanceID)))
            {
                return;
            }

            string sql = @"DELETE FROM dbo.Reimb WHERE FlowInstanceID = @FlowInstanceID";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, flowInstanceID);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                Common.Helper.Instance.SetException(ex);
            }
        }
    }
}